package com.yuebao.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class JsonUtils {
    public static ObjectMapper mapper = new ObjectMapper();
    
    public JsonUtils() {
    }
    
    public static <K, V> Optional<Map<K, V>> decodeJsonToMap(String json) {
        if (StringUtils.isEmpty(json)) {
            return Optional.empty();
        } else {
            try {
                Map<K, V> obj = (Map)mapper.readValue(json, new TypeReference<Map<K, V>>() {
                });
                return Optional.of(obj);
            } catch (IOException var2) {
                var2.printStackTrace();
                return Optional.empty();
            }
        }
    }
    
    public static <V> Optional<List<V>> decodeJsonToList(String json) {
        if (StringUtils.isEmpty(json)) {
            return Optional.empty();
        } else {
            try {
                List<V> obj = (List)mapper.readValue(json, new TypeReference<List<V>>() {
                });
                return Optional.of(obj);
            } catch (IOException var2) {
                var2.printStackTrace();
                return Optional.empty();
            }
        }
    }
    
    public static <V> Optional<List<V>> decodeJsonToList(String json, Class<V> clazz) {
        if (StringUtils.isEmpty(json)) {
            return Optional.empty();
        } else {
            try {
                List<V> obj = (List)mapper.readValue(json, getCollectionType(ArrayList.class, clazz));
                return Optional.of(obj);
            } catch (IOException var3) {
                var3.printStackTrace();
                return Optional.empty();
            }
        }
    }
    
    public static <V> Optional<Set<V>> decodeJsonToSet(String json) {
        if (StringUtils.isEmpty(json)) {
            return Optional.empty();
        } else {
            try {
                Set<V> obj = (Set)mapper.readValue(json, new TypeReference<Set<V>>() {
                });
                return Optional.of(obj);
            } catch (IOException var2) {
                var2.printStackTrace();
                return Optional.empty();
            }
        }
    }
    
    public static <V> Optional<Set<V>> decodeJsonToSet(String json, Class<V> clazz) {
        if (StringUtils.isEmpty(json)) {
            return Optional.empty();
        } else {
            try {
                Set<V> obj = (Set)mapper.readValue(json, getCollectionType(HashSet.class, clazz));
                return Optional.of(obj);
            } catch (IOException var3) {
                var3.printStackTrace();
                return Optional.empty();
            }
        }
    }
    
    public static <T> Optional<T> readValue(String json, Class<T> clazz) {
        if (!StringUtils.isEmpty(json) && clazz != null) {
            try {
                return Optional.of(mapper.readValue(json, clazz));
            } catch (IOException var3) {
                var3.printStackTrace();
                return Optional.empty();
            }
        } else {
            return Optional.empty();
        }
    }
    
    public static String writeValueAsString(Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException var2) {
            var2.printStackTrace();
            return "";
        }
    }
    
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }
    
    static {
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.registerModule(new JavaTimeModule());
    }
}
